package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021-7-17.<br><br>
 *
 * 最小操作次数使数组元素相等
 */
public class Topic453 {

    public static void main(String[] args) {
        Topic453 instance = new Topic453();

        int[] array = new int[] {1,2,3};
        System.out.println(instance.minMoves(array));

        array = new int[] {1,1,1};
        System.out.println(instance.minMoves(array));
    }

    public int minMoves(int[] nums) {
        int min = nums[0];
        int sum = 0;
        for(int value : nums) {
            if(value < min) {
                min = value;
            }
            sum += value;
        }

        return sum - min * nums.length;
    }

    public int minMoves2(int[] nums) {
        int min = nums[0];
        for(int value : nums) {
            if(value < min) {
                min = value;
            }
        }

        int counter = 0;
        for(int value : nums) {
            counter += value - min;
        }

        return counter;
    }
} 
